<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notification extends CI_Controller 
{	
	private $_data;
	
  	function __construct()
  	{
  		parent::__construct();
  		$this->load->model('notificationModel');
  	}
	
	public function index($page = 1) 
	{
		$this->load->library('pagination');
		$this->load->library('pnkpagination');
		
		$config = $this->pnkpagination->initConfig('notification', $this->notificationModel);
		
		$this->pagination->initialize($config);
		$offset = --$page * $config['per_page'];
   		$data['notifications'] = $this->notificationModel->getNotifications($config['per_page'], $offset);
		
		$this->_data['content'] = $this->load->view('backend/notification/index', $data, true);
  	    $this->load->view('layout/backend/index', $this->_data);
	}
  	
	public function add()
	{
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('name', 'Tên thông báo', 'required|is_unique[notifications.name]');
		$this->form_validation->set_rules('content_short', 'Nội dung thông báo', 'required|max_length[128]');
		
	  	if ($this->form_validation->run() == FALSE) { // not validate form 	  
		  	$this->_data['content'] = $this->load->view('backend/notification/add', '', true);
  	      	$this->load->view('layout/backend/index', $this->_data);
	  	} else {
	  		$data = array(
	  	  	'content_short' => $this->input->post('content_short'),
	  	  	);
	  	  	$this->notificationModel->add($_POST);
	  	  	$this->session->set_flashdata('message', 'Đã thêm mới thành công!');
	  	  	redirect('backend/notification/index');
	  	}
	}
	
	public function edit($id = NUll)
	{
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('name', 'Tên thông báo', 'required');
		$this->form_validation->set_rules('content_short', 'Nội dung thông báo', 'required|max_length[128]');
		
	  	if ($this->form_validation->run() == FALSE) { // not validate form 	
			$data['notification'] = $this->notificationModel->getNotificationById($id);

		  	$this->_data['content'] = $this->load->view('backend/notification/edit', $data, true);
  	      	$this->load->view('layout/backend/index', $this->_data);
	  	} else {
		  	
	  		$data = array(
		  	'content_short' => $this->input->post('content_short'),
		  	);
		  	
  	  		$this->notificationModel->update($id, $_POST);
  	  		$this->session->set_flashdata('message', 'Đã sửa thành công!');
	  	  	redirect('backend/notification/index');
	  	}
	}
	
	public function delete($id)
  	{
    	$this->notificationModel->delete($id);
    	$this->session->set_flashdata('message', 'Đã xóa thành công!');
  		redirect('backend/notification/index');
  	} 
  
}